<?php
/**
 * Created by PhpStorm.
 * User: 7howe
 * Date: 2017/8/14
 * Time: 22:23
 */

//注册
use Addons\QyWechat\Services\ProvidersApi;
use Addons\QyWechat\Services\ThirdProvider;
use Addons\QyWechat\Services\QyWechatApi;
use Addons\QyWechat\Helper;
use EasyWeChat\Factory;
$domain=$_SERVER['HTTP_HOST'];
$agency=new Model('user_agency');
$agency->find(array('domain'=>$domain));
$is_scan=$agency->is_scan;
$thirdappsetting=(new Model('qy_agency_thirdappsetting'))->find(['pid'=>$agency->id]);
if($thirdappsetting->is_type == 1){
    $config = [
        'corp_id'          => $thirdappsetting->corpid?$thirdappsetting->corpid:'',
        'secret'           => $thirdappsetting->providerSecret?$thirdappsetting->providerSecret:'',
        'suite_id'         => $thirdappsetting->suite_id?$thirdappsetting->suite_id:'',
        'suite_secret'     => $thirdappsetting->suite_secret?$thirdappsetting->suite_secret:'',
        'token'            => $thirdappsetting->token?$thirdappsetting->token:'',
        'aes_key'          => $thirdappsetting->encodingAESKey?$thirdappsetting->encodingAESKey:'',
        'reg_template_id'  => $thirdappsetting->template_id?$thirdappsetting->template_id:'',
    ];
    $app = Factory::openWork($config);
    $RegisterCode = $app->provider->getRegisterCode();
    $RegisterUrl = "https://open.work.weixin.qq.com/3rdservice/wework/register?register_code={$RegisterCode['register_code']}";
    Redirect::to($RegisterUrl);
}
$domain=$_SERVER['HTTP_HOST'];
$agency=new Model('user_agency');
$agency->find(array('domain'=>$domain));
$register_check=$agency->register_check;
if(Request::post('register')=='register'){
    $ut= new Model('user_register');
    $ut->find(1);
    if($ut->has_id()){
        $registerlevel=$ut->registerlevel;
        $registerdays=$ut->registerdays;
        $registerisstop=$ut->registerisstop;
    }else{
        $registerlevel=1;
        $registerdays=7;
        $registerisstop=0;
    }
    $domain=$_SERVER['HTTP_HOST'];
    $agency=new Model('user_agency');
    $agency->find(array('domain'=>$domain));
    $agid=$agency->id;
    $pagetype=$agency->user_pc;
    $register_check=$agency->register_check;
        // if(!$agency->isadmin){//当前用户不是管理员
        //     $agencyCheck=new Model('user_agency');
        //     $agencyCheck->find($agency->pid);//代理商上级用户
        //     if($agencyCheck->isadmin && !$agency->isyg ){//如果是一级代理并且不是员工
        //         //当前一级代理允许开通的用户量
        //         $taoshu = new Model('agency_taoshu');
        //         $jgres = $taoshu->find($agency->level);
        //         $usercount=$jgres->usercount+$agency->taoshu;
        //         //一级代理的用户量
        //         $users=new Model('user');
        //         $counts=$users->where(array('agid'=>$agency->id))->count();
        //         //一级代理下级代理开通的总套数加上开通的用户量总和
        //         $allTaosu=totals($agency->id)+$counts;
        //         if($jgres->usercount!=0){//如果限制用户量的话
        //             if($usercount<=$allTaosu){
        //                 //tusi('当前网站用户量已达上限');
        //                 echo '<script type="text/javascript">alert("当前网站用户量已达上限!");</script>';
        //                 exit;
        //                 $taoshuCheck=1;
        //             }
        //         }
        //     }else if(!$agencyCheck->isadmin){//不是一级代理
        //         $users=new Model('user');
        //         $counts=$users->where(array('agid'=>$agency->id))->count();
        //         //代理商下级代理开通的总套数加上开通的用户量总和
        //         $allTaosu=totals($agency->id)+$counts;
        //         if($allTaosu>=$agency->taoshu){
        //             //tusi('当前网站用户量已达上限');
        //             echo '<script type="text/javascript">alert("当前网站用户量已达上限!");</script>';
        //             exit;
        //             $taoshuCheck=1;
        //         }
        //     }
        // }
        $mu = new User();
        $mu->un = Request::post('telephone');
        $mu->pwd = Request::post('pwd');
        $mu->email = $reg->email;
        $mu->telephone = Request::post('telephone');
        $mu->agid=$agid;
        $mu->pagetype=$pagetype;
        //默认注册用户关闭，如果新注册用户需要审核，请把此项注释去掉(暂时不用)
        //$mu->isstop = 1;

        //获取后台设置是否审核
        $mu->isstop=$registerisstop;

        //注册用户时间
        $mu->rtime = date('Y-m-d H:i:s',time());
        //用户试用时间为1周(暂时不用)
        //$mu->mendtime = date('Y-m-d h:i:s',strtotime('+1 week'));

        //获取后台设置天数
        /**if($registerlevel!=1){
        $mu->mendtime=date('Y-m-d',time()+86400*$registerdays);
        }*/
        $mu->mendtime=date('Y-m-d',time()+86400*$registerdays);

        //获取后台设置级别
        $mu->level_id =$registerlevel;

        $yzm=Request::post('yzm');
        $code = Cache::get('sms_code');
        $telephone=Request::post('telephone');
        $pwd=Request::post('pwd');
        if($taoshuCheck!=1){
           if($register_check==1&&($yzm==null||$yzm!=$code)){
               //tusi('验证码错误');
               echo '<script type="text/javascript">alert("验证码错误!");history.go(-1);</script>';
               exit;
           }elseif(!empty($telephone) && !empty($pwd) && !$mu->is_real('un')) {
            $mu->pwd = md5(Request::post('pwd'));
                $info = $mu->save();
                if($mu->errors()){
                    foreach($mu->errors() as $item){
                        $msg .= implode(',',array_values($item));
                    }
                }
                $msg = $msg ?  : '注册已成功!' ;
                //tusi($msg);history.go('/login');
                echo "<script>alert('".$msg."'); window.location.href='login.html';</script>";
                //$info && Redirect::delay_to('/login',3);
            } elseif ($mu->is_real('un')) {
                //tusi('用户名已存在，请重新填写');
                echo '<script type="text/javascript">alert("用户名已存在，请重新填写");history.go(-1);</script>';
                exit;
            }
        }

    
}
//查询当前代理商无限极下级代理总套数
function totals($id){
    static $all;
    $agency=new Model('user_agency');   
    $aglist=$agency->where(array('pid'=>$id))->order('id')->list_all();
    foreach($aglist as $as){
        if($as->id){
            $taoshu=$as->taoshu;
            $all=$all+$taoshu;
            totals($as->id);            
        }
    }
    return $all;
}

